/**
 * 
 */
package lastfmbaseball.core;

import java.util.*;

/**
 * @author DanBress
 * 
 */
public class TeamInGame
{
	private Team team;
	private List<PlayerInGame> allPlayersInGame;
	private List<PlayerInGame> battingLineup;

	public TeamInGame(Team team, List<PlayerInGame> allPlayersInGame,
			List<PlayerInGame> battingLineup)
	{
		this.team = team;
		this.allPlayersInGame = allPlayersInGame;
		this.battingLineup = battingLineup;
	}

	public void changePitcher(PlayerInGame newPitcher)
	{
		PlayerInGame currentPitcher = getPitcher();
		currentPitcher.setState(PlayerInGameState.Unavailable);
		newPitcher.setState(PlayerInGameState.InGamePitching);
	}

	public List<PlayerInGame> getAllPlayersInGame()
	{
		return this.allPlayersInGame;
	}

	public List<PlayerInGame> getBattingLineup()
	{
		return battingLineup;
	}

	public PlayerInGame getPitcher()
	{
		PlayerInGame pitcher = null;

		for (int i = 0; i < allPlayersInGame.size(); i++)
		{
			PlayerInGame player = allPlayersInGame.get(i);
			if (player.getState() == PlayerInGameState.InGamePitching)
			{
				pitcher = player;
			}
		}

		return pitcher;
	}

	public Team getTeam()
	{
		return team;
	}

	public void pinchHit(PlayerInGame pinchHitter, PlayerInGame playerOut)
	{
		pinchHitter.setState(PlayerInGameState.PinchHitter);
		playerOut.setState(PlayerInGameState.Unavailable);

		int currentIndex = battingLineup.indexOf(playerOut);
		battingLineup.remove(currentIndex);
		battingLineup.add(currentIndex, pinchHitter);
	}

	public String toString()
	{
		String result = "";

		result += team.toString() + "\r\n";

		for (int i = 0; i < allPlayersInGame.size(); i++)
		{
			result += allPlayersInGame.get(i).toString() + "\r\n";
		}

		return result;
	}

}
